<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">

<title>module Random::Formatter - random: Ruby Standard Library Documentation</title>


<script src="../js/navigation.js" defer></script>
<script src="../js/search.js" defer></script>
<script src="../js/search_index.js" defer></script>
<script src="../js/searcher.js" defer></script>
<script src="../js/darkfish.js" defer></script>

<script src="../js/jquery-3.2.0.min.js"></script>

<script src="../js/vue.min.js"></script>
<script src="../js/js.cookie.min.js"></script>

<link href="../css/fonts.css" rel="stylesheet">
<link id='rdoccss' href="../css/rdoc.css" rel="stylesheet">
<link href="../css/carbon17.css" rel="stylesheet">

<script type="text/javascript">
  var rdoc_rel_prefix = "../";
  var index_rel_prefix = "../";
  var darkModeCsseHref = "../css/rdoc-dm.css"
  var defaultModeCssHref = "../css/rdoc.css"
  // var cssDarkmode = Cookies.get('darkmode');
  
  if( Cookies.get("darkmode") == "true") {
	$('#rdoccss').attr("href", darkModeCsseHref);
}

//  https://cssdeck.com/blog/simple-jquery-stylesheet-switcher/

document.write('<style type="text/css">body{display:none}</style>');

</script>


</head>
<body id="top" role="document" class="module">
  <!-- this is class.html -->

  <div id='actionbar' >
    <div class='wrapper mdiv'>
      <ul class='grids g0'></ul>
    </div> 
    <!-- VERSION HEADER for 3.3.0.preview2 NOT FOUND -->
  </div> <!-- end action bar -->

  <div class='wrapper hdiv'>

    


    <nav id='vapp' role="navigation">
    <div id="project-navigation">
      <div id="home-section" role="region" title="Quick navigation" class="nav-section">
  <h2><a href="../index.html" rel="home">Home</a></h2>

  <div id="table-of-contents-navigation"  >
    <a href="../table_of_contents.html#pages">Pages</a>
    <a href="../table_of_contents.html#classes">Classes</a>
    <a href="../table_of_contents.html#methods">Methods</a>
  </div>
</div>

      <div id="search-section" role="search" class="project-section initially-hidden">
  <form action="#" method="get" accept-charset="utf-8">
    <div id="search-field-wrapper">
      <input id="search-field" role="combobox" aria-label="Search"
             aria-autocomplete="list" aria-controls="search-results"
             type="text" name="search" placeholder="Search" spellcheck="false"
             title="Type to search, Up and Down to navigate, Enter to load">
    </div>

    <ul id="search-results" aria-label="Search Results"
        aria-busy="false" aria-expanded="false"
        aria-atomic="false" class="initially-hidden"></ul>
  </form>
</div>

    </div>


    
<div class="nav-section">
  <h3>Table of Contents</h3>

  <ul class="link-list" role="directory">
    <li><a href="#module-Random::Formatter-label-Random+number+formatter.">Random number formatter.</a>
    <li><a href="#module-Random::Formatter-label-Examples">Examples</a>
  </ul>
</div>


    <button id='toggleThing' @click="toggleNav()" >Show/hide navigation</button>
    <div :class="isOpen ? 'block' : 'hidden' " id='toggleMe'>
      <div id="class-metadata">
        
        
        
        
        
<!-- Method Quickref -->
<div id="method-list-section" class="nav-section">
  <h3>Methods</h3>

  <ul class="link-list" role="directory">
    <li ><a href="#method-i-alphanumeric">#alphanumeric</a>
    <li ><a href="#method-i-base64">#base64</a>
    <li ><a href="#method-i-choose">#choose</a>
    <li ><a href="#method-i-gen_random">#gen_random</a>
    <li ><a href="#method-i-hex">#hex</a>
    <li ><a href="#method-i-random_bytes">#random_bytes</a>
    <li ><a href="#method-i-urlsafe_base64">#urlsafe_base64</a>
    <li ><a href="#method-i-uuid">#uuid</a>
  </ul>
</div>

      </div>
     </div>
    </nav>


    <div id='extraz'><div class='adzbox-index'  >
      
     </div>         
    </div>

    <main role="main" aria-labelledby="module-Random::Formatter">
    <h1 id="module-Random::Formatter" class="module">
      module Random::Formatter
    </h1>

    <section class="description">
    
<h2 id="module-Random::Formatter-label-Random+number+formatter.">Random number formatter.<span><a href="#module-Random::Formatter-label-Random+number+formatter.">&para;</a> <a href="#top">&uarr;</a></span></h2>

<p>Formats generated random numbers in many manners. When <code>&#39;random/formatter&#39;</code> is required, several methods are added to empty core module <code>Random::Formatter</code>, making them available as Random’s instance and module methods.</p>

<p>Standard library SecureRandom is also extended with the module, and the methods described below are available as a module methods in it.</p>

<h3 id="module-Random::Formatter-label-Examples">Examples<span><a href="#module-Random::Formatter-label-Examples">&para;</a> <a href="#top">&uarr;</a></span></h3>

<p>Generate random hexadecimal strings:</p>

<pre class="ruby"><span class="ruby-identifier">require</span> <span class="ruby-string">&#39;random/formatter&#39;</span>

<span class="ruby-identifier">prng</span> = <span class="ruby-constant">Random</span>.<span class="ruby-identifier">new</span>
<span class="ruby-identifier">prng</span>.<span class="ruby-identifier">hex</span>(<span class="ruby-value">10</span>) <span class="ruby-comment">#=&gt; &quot;52750b30ffbc7de3b362&quot;</span>
<span class="ruby-identifier">prng</span>.<span class="ruby-identifier">hex</span>(<span class="ruby-value">10</span>) <span class="ruby-comment">#=&gt; &quot;92b15d6c8dc4beb5f559&quot;</span>
<span class="ruby-identifier">prng</span>.<span class="ruby-identifier">hex</span>(<span class="ruby-value">13</span>) <span class="ruby-comment">#=&gt; &quot;39b290146bea6ce975c37cfc23&quot;</span>
<span class="ruby-comment"># or just</span>
<span class="ruby-constant">Random</span>.<span class="ruby-identifier">hex</span> <span class="ruby-comment">#=&gt; &quot;1aed0c631e41be7f77365415541052ee&quot;</span>
</pre>

<p>Generate random base64 strings:</p>

<pre class="ruby"><span class="ruby-identifier">prng</span>.<span class="ruby-identifier">base64</span>(<span class="ruby-value">10</span>) <span class="ruby-comment">#=&gt; &quot;EcmTPZwWRAozdA==&quot;</span>
<span class="ruby-identifier">prng</span>.<span class="ruby-identifier">base64</span>(<span class="ruby-value">10</span>) <span class="ruby-comment">#=&gt; &quot;KO1nIU+p9DKxGg==&quot;</span>
<span class="ruby-identifier">prng</span>.<span class="ruby-identifier">base64</span>(<span class="ruby-value">12</span>) <span class="ruby-comment">#=&gt; &quot;7kJSM/MzBJI+75j8&quot;</span>
<span class="ruby-constant">Random</span>.<span class="ruby-identifier">base64</span>(<span class="ruby-value">4</span>) <span class="ruby-comment">#=&gt; &quot;bsQ3fQ==&quot;</span>
</pre>

<p>Generate random binary strings:</p>

<pre class="ruby"><span class="ruby-identifier">prng</span>.<span class="ruby-identifier">random_bytes</span>(<span class="ruby-value">10</span>) <span class="ruby-comment">#=&gt; &quot;\016\t{\370g\310pbr\301&quot;</span>
<span class="ruby-identifier">prng</span>.<span class="ruby-identifier">random_bytes</span>(<span class="ruby-value">10</span>) <span class="ruby-comment">#=&gt; &quot;\323U\030TO\234\357\020\a\337&quot;</span>
<span class="ruby-constant">Random</span>.<span class="ruby-identifier">random_bytes</span>(<span class="ruby-value">6</span>) <span class="ruby-comment">#=&gt; &quot;\xA1\xE6Lr\xC43&quot;</span>
</pre>

<p>Generate alphanumeric strings:</p>

<pre class="ruby"><span class="ruby-identifier">prng</span>.<span class="ruby-identifier">alphanumeric</span>(<span class="ruby-value">10</span>) <span class="ruby-comment">#=&gt; &quot;S8baxMJnPl&quot;</span>
<span class="ruby-identifier">prng</span>.<span class="ruby-identifier">alphanumeric</span>(<span class="ruby-value">10</span>) <span class="ruby-comment">#=&gt; &quot;aOxAg8BAJe&quot;</span>
<span class="ruby-constant">Random</span>.<span class="ruby-identifier">alphanumeric</span> <span class="ruby-comment">#=&gt; &quot;TmP9OsJHJLtaZYhP&quot;</span>
</pre>

<p>Generate UUIDs:</p>

<pre class="ruby"><span class="ruby-identifier">prng</span>.<span class="ruby-identifier">uuid</span> <span class="ruby-comment">#=&gt; &quot;2d931510-d99f-494a-8c67-87feb05e1594&quot;</span>
<span class="ruby-identifier">prng</span>.<span class="ruby-identifier">uuid</span> <span class="ruby-comment">#=&gt; &quot;bad85eb9-0713-4da7-8d36-07a8e4b00eab&quot;</span>
<span class="ruby-constant">Random</span>.<span class="ruby-identifier">uuid</span> <span class="ruby-comment">#=&gt; &quot;f14e0271-de96-45cc-8911-8910292a42cd&quot;</span>
</pre>

<p>All methods are available in the standard library SecureRandom, too:</p>

<pre class="ruby"><span class="ruby-constant">SecureRandom</span>.<span class="ruby-identifier">hex</span> <span class="ruby-comment">#=&gt; &quot;05b45376a30c67238eb93b16499e50cf&quot;</span>
</pre>

    </section>

      <section id="5Buntitled-5D" class="documentation-section">


      <section class="constants-list">
      <header>
      <h3>Constants</h3>
      </header>
      <dl>
          <dt id="ALPHANUMERIC">ALPHANUMERIC
          <dd>
        </dl>
        </section>



                <section id="public-instance-5Buntitled-5D-method-details" class="method-section">
                <header>
                <h3>Public Instance Methods</h3>
                </header>

                  <div id="method-i-alphanumeric" class="method-detail ">
                            <div class="method-heading">
                              <span class="method-name">alphanumeric</span><span
                                class="method-args">(n = nil, chars: ALPHANUMERIC)</span>
                              <span class="method-click-advice">click to toggle source</span>
                            </div>

                            <div class="method-description">
                              <p>Generate a random alphanumeric string.</p>

<p>The argument <em>n</em> specifies the length, in characters, of the alphanumeric string to be generated. The argument <em>chars</em> specifies the character list which the result is consist of.</p>

<p>If <em>n</em> is not specified or is nil, 16 is assumed. It may be larger in the future.</p>

<p>The result may contain A-Z, a-z and 0-9, unless <em>chars</em> is specified.</p>

<pre class="ruby"><span class="ruby-identifier">require</span> <span class="ruby-string">&#39;random/formatter&#39;</span>

<span class="ruby-constant">Random</span>.<span class="ruby-identifier">alphanumeric</span>     <span class="ruby-comment">#=&gt; &quot;2BuBuLf3WfSKyQbR&quot;</span>
<span class="ruby-comment"># or</span>
<span class="ruby-identifier">prng</span> = <span class="ruby-constant">Random</span>.<span class="ruby-identifier">new</span>
<span class="ruby-identifier">prng</span>.<span class="ruby-identifier">alphanumeric</span>(<span class="ruby-value">10</span>) <span class="ruby-comment">#=&gt; &quot;i6K93NdqiH&quot;</span>

<span class="ruby-constant">Random</span>.<span class="ruby-identifier">alphanumeric</span>(<span class="ruby-value">4</span>, <span class="ruby-value">chars:</span> [<span class="ruby-operator">*</span><span class="ruby-string">&quot;0&quot;</span><span class="ruby-operator">..</span><span class="ruby-string">&quot;9&quot;</span>]) <span class="ruby-comment">#=&gt; &quot;2952&quot;</span>
<span class="ruby-comment"># or</span>
<span class="ruby-identifier">prng</span> = <span class="ruby-constant">Random</span>.<span class="ruby-identifier">new</span>
<span class="ruby-identifier">prng</span>.<span class="ruby-identifier">alphanumeric</span>(<span class="ruby-value">10</span>, <span class="ruby-value">chars:</span> [<span class="ruby-operator">*</span><span class="ruby-string">&quot;!&quot;</span><span class="ruby-operator">..</span><span class="ruby-string">&quot;/&quot;</span>]) <span class="ruby-comment">#=&gt; &quot;,.,++%/&#39;&#39;.&quot;</span>
</pre>

                              <div class="method-source-code" id="alphanumeric-source">
            <pre><span class="ruby-comment"># File random/formatter.rb, line 247</span>
<span class="ruby-keyword">def</span> <span class="ruby-identifier ruby-title">alphanumeric</span>(<span class="ruby-identifier">n</span> = <span class="ruby-keyword">nil</span>, <span class="ruby-value">chars:</span> <span class="ruby-constant">ALPHANUMERIC</span>)
  <span class="ruby-identifier">n</span> = <span class="ruby-value">16</span> <span class="ruby-keyword">if</span> <span class="ruby-identifier">n</span>.<span class="ruby-identifier">nil?</span>
  <span class="ruby-identifier">choose</span>(<span class="ruby-identifier">chars</span>, <span class="ruby-identifier">n</span>)
<span class="ruby-keyword">end</span></pre>
                              </div>
                            </div>


                          </div>

                  <div id="method-i-base64" class="method-detail ">
                            <div class="method-heading">
                              <span class="method-name">base64</span><span
                                class="method-args">(n=nil)</span>
                              <span class="method-click-advice">click to toggle source</span>
                            </div>

                            <div class="method-description">
                              <p>Generate a random base64 string.</p>

<p>The argument <em>n</em> specifies the length, in bytes, of the random number to be generated. The length of the result string is about 4/3 of <em>n</em>.</p>

<p>If <em>n</em> is not specified or is nil, 16 is assumed. It may be larger in the future.</p>

<p>The result may contain A-Z, a-z, 0-9, “+”, “/” and “=”.</p>

<pre class="ruby"><span class="ruby-identifier">require</span> <span class="ruby-string">&#39;random/formatter&#39;</span>

<span class="ruby-constant">Random</span>.<span class="ruby-identifier">base64</span> <span class="ruby-comment">#=&gt; &quot;/2BuBuLf3+WfSKyQbRcc/A==&quot;</span>
<span class="ruby-comment"># or</span>
<span class="ruby-identifier">prng</span> = <span class="ruby-constant">Random</span>.<span class="ruby-identifier">new</span>
<span class="ruby-identifier">prng</span>.<span class="ruby-identifier">base64</span> <span class="ruby-comment">#=&gt; &quot;6BbW0pxO0YENxn38HMUbcQ==&quot;</span>
</pre>

<p>See RFC 3548 for the definition of base64.</p>

                              <div class="method-source-code" id="base64-source">
            <pre><span class="ruby-comment"># File random/formatter.rb, line 114</span>
<span class="ruby-keyword">def</span> <span class="ruby-identifier ruby-title">base64</span>(<span class="ruby-identifier">n</span>=<span class="ruby-keyword">nil</span>)
  [<span class="ruby-identifier">random_bytes</span>(<span class="ruby-identifier">n</span>)].<span class="ruby-identifier">pack</span>(<span class="ruby-string">&quot;m0&quot;</span>)
<span class="ruby-keyword">end</span></pre>
                              </div>
                            </div>


                          </div>

                  <div id="method-i-hex" class="method-detail ">
                            <div class="method-heading">
                              <span class="method-name">hex</span><span
                                class="method-args">(n=nil)</span>
                              <span class="method-click-advice">click to toggle source</span>
                            </div>

                            <div class="method-description">
                              <p>Generate a random hexadecimal string.</p>

<p>The argument <em>n</em> specifies the length, in bytes, of the random number to be generated. The length of the resulting hexadecimal string is twice of <em>n</em>.</p>

<p>If <em>n</em> is not specified or is nil, 16 is assumed. It may be larger in the future.</p>

<p>The result may contain 0-9 and a-f.</p>

<pre class="ruby"><span class="ruby-identifier">require</span> <span class="ruby-string">&#39;random/formatter&#39;</span>

<span class="ruby-constant">Random</span>.<span class="ruby-identifier">hex</span> <span class="ruby-comment">#=&gt; &quot;eb693ec8252cd630102fd0d0fb7c3485&quot;</span>
<span class="ruby-comment"># or</span>
<span class="ruby-identifier">prng</span> = <span class="ruby-constant">Random</span>.<span class="ruby-identifier">new</span>
<span class="ruby-identifier">prng</span>.<span class="ruby-identifier">hex</span> <span class="ruby-comment">#=&gt; &quot;91dc3bfb4de5b11d029d376634589b61&quot;</span>
</pre>

                              <div class="method-source-code" id="hex-source">
            <pre><span class="ruby-comment"># File random/formatter.rb, line 92</span>
<span class="ruby-keyword">def</span> <span class="ruby-identifier ruby-title">hex</span>(<span class="ruby-identifier">n</span>=<span class="ruby-keyword">nil</span>)
  <span class="ruby-identifier">random_bytes</span>(<span class="ruby-identifier">n</span>).<span class="ruby-identifier">unpack1</span>(<span class="ruby-string">&quot;H*&quot;</span>)
<span class="ruby-keyword">end</span></pre>
                              </div>
                            </div>


                          </div>

                  <div id="method-i-random_bytes" class="method-detail ">
                            <div class="method-heading">
                              <span class="method-name">random_bytes</span><span
                                class="method-args">(n=nil)</span>
                              <span class="method-click-advice">click to toggle source</span>
                            </div>

                            <div class="method-description">
                              <p>Generate a random binary string.</p>

<p>The argument <em>n</em> specifies the length of the result string.</p>

<p>If <em>n</em> is not specified or is nil, 16 is assumed. It may be larger in future.</p>

<p>The result may contain any byte: “x00” - “xff”.</p>

<pre class="ruby"><span class="ruby-identifier">require</span> <span class="ruby-string">&#39;random/formatter&#39;</span>

<span class="ruby-constant">Random</span>.<span class="ruby-identifier">random_bytes</span> <span class="ruby-comment">#=&gt; &quot;\xD8\\\xE0\xF4\r\xB2\xFC*WM\xFF\x83\x18\xF45\xB6&quot;</span>
<span class="ruby-comment"># or</span>
<span class="ruby-identifier">prng</span> = <span class="ruby-constant">Random</span>.<span class="ruby-identifier">new</span>
<span class="ruby-identifier">prng</span>.<span class="ruby-identifier">random_bytes</span> <span class="ruby-comment">#=&gt; &quot;m\xDC\xFC/\a\x00Uf\xB2\xB2P\xBD\xFF6S\x97&quot;</span>
</pre>

                              <div class="method-source-code" id="random_bytes-source">
            <pre><span class="ruby-comment"># File random/formatter.rb, line 71</span>
<span class="ruby-keyword">def</span> <span class="ruby-identifier ruby-title">random_bytes</span>(<span class="ruby-identifier">n</span>=<span class="ruby-keyword">nil</span>)
  <span class="ruby-identifier">n</span> = <span class="ruby-identifier">n</span> <span class="ruby-operator">?</span> <span class="ruby-identifier">n</span>.<span class="ruby-identifier">to_int</span> <span class="ruby-operator">:</span> <span class="ruby-value">16</span>
  <span class="ruby-identifier">gen_random</span>(<span class="ruby-identifier">n</span>)
<span class="ruby-keyword">end</span></pre>
                              </div>
                            </div>


                          </div>

                  <div id="method-i-urlsafe_base64" class="method-detail ">
                            <div class="method-heading">
                              <span class="method-name">urlsafe_base64</span><span
                                class="method-args">(n=nil, padding=false)</span>
                              <span class="method-click-advice">click to toggle source</span>
                            </div>

                            <div class="method-description">
                              <p>Generate a random URL-safe base64 string.</p>

<p>The argument <em>n</em> specifies the length, in bytes, of the random number to be generated. The length of the result string is about 4/3 of <em>n</em>.</p>

<p>If <em>n</em> is not specified or is nil, 16 is assumed. It may be larger in the future.</p>

<p>The boolean argument <em>padding</em> specifies the padding. If it is false or nil, padding is not generated. Otherwise padding is generated. By default, padding is not generated because “=” may be used as a URL delimiter.</p>

<p>The result may contain A-Z, a-z, 0-9, “-” and “_”. “=” is also used if <em>padding</em> is true.</p>

<pre class="ruby"><span class="ruby-identifier">require</span> <span class="ruby-string">&#39;random/formatter&#39;</span>

<span class="ruby-constant">Random</span>.<span class="ruby-identifier">urlsafe_base64</span> <span class="ruby-comment">#=&gt; &quot;b4GOKm4pOYU_-BOXcrUGDg&quot;</span>
<span class="ruby-comment"># or</span>
<span class="ruby-identifier">prng</span> = <span class="ruby-constant">Random</span>.<span class="ruby-identifier">new</span>
<span class="ruby-identifier">prng</span>.<span class="ruby-identifier">urlsafe_base64</span> <span class="ruby-comment">#=&gt; &quot;UZLdOkzop70Ddx-IJR0ABg&quot;</span>

<span class="ruby-identifier">prng</span>.<span class="ruby-identifier">urlsafe_base64</span>(<span class="ruby-keyword">nil</span>, <span class="ruby-keyword">true</span>) <span class="ruby-comment">#=&gt; &quot;i0XQ-7gglIsHGV2_BNPrdQ==&quot;</span>
<span class="ruby-identifier">prng</span>.<span class="ruby-identifier">urlsafe_base64</span>(<span class="ruby-keyword">nil</span>, <span class="ruby-keyword">true</span>) <span class="ruby-comment">#=&gt; &quot;-M8rLhr7JEpJlqFGUMmOxg==&quot;</span>
</pre>

<p>See RFC 3548 for the definition of URL-safe base64.</p>

                              <div class="method-source-code" id="urlsafe_base64-source">
            <pre><span class="ruby-comment"># File random/formatter.rb, line 145</span>
<span class="ruby-keyword">def</span> <span class="ruby-identifier ruby-title">urlsafe_base64</span>(<span class="ruby-identifier">n</span>=<span class="ruby-keyword">nil</span>, <span class="ruby-identifier">padding</span>=<span class="ruby-keyword">false</span>)
  <span class="ruby-identifier">s</span> = [<span class="ruby-identifier">random_bytes</span>(<span class="ruby-identifier">n</span>)].<span class="ruby-identifier">pack</span>(<span class="ruby-string">&quot;m0&quot;</span>)
  <span class="ruby-identifier">s</span>.<span class="ruby-identifier">tr!</span>(<span class="ruby-string">&quot;+/&quot;</span>, <span class="ruby-string">&quot;-_&quot;</span>)
  <span class="ruby-identifier">s</span>.<span class="ruby-identifier">delete!</span>(<span class="ruby-string">&quot;=&quot;</span>) <span class="ruby-keyword">unless</span> <span class="ruby-identifier">padding</span>
  <span class="ruby-identifier">s</span>
<span class="ruby-keyword">end</span></pre>
                              </div>
                            </div>


                          </div>

                  <div id="method-i-uuid" class="method-detail ">
                            <div class="method-heading">
                              <span class="method-name">uuid</span><span
                                class="method-args">()</span>
                              <span class="method-click-advice">click to toggle source</span>
                            </div>

                            <div class="method-description">
                              <p>Generate a random v4 UUID (Universally Unique IDentifier).</p>

<pre class="ruby"><span class="ruby-identifier">require</span> <span class="ruby-string">&#39;random/formatter&#39;</span>

<span class="ruby-constant">Random</span>.<span class="ruby-identifier">uuid</span> <span class="ruby-comment">#=&gt; &quot;2d931510-d99f-494a-8c67-87feb05e1594&quot;</span>
<span class="ruby-constant">Random</span>.<span class="ruby-identifier">uuid</span> <span class="ruby-comment">#=&gt; &quot;bad85eb9-0713-4da7-8d36-07a8e4b00eab&quot;</span>
<span class="ruby-comment"># or</span>
<span class="ruby-identifier">prng</span> = <span class="ruby-constant">Random</span>.<span class="ruby-identifier">new</span>
<span class="ruby-identifier">prng</span>.<span class="ruby-identifier">uuid</span> <span class="ruby-comment">#=&gt; &quot;62936e70-1815-439b-bf89-8492855a7e6b&quot;</span>
</pre>

<p>The version 4 UUID is purely random (except the version). It doesn’t contain meaningful information such as MAC addresses, timestamps, etc.</p>

<p>The result contains 122 random bits (15.25 random bytes).</p>

<p>See <a href="https://datatracker.ietf.org/doc/html/rfc4122">RFC4122</a> for details of UUID.</p>

                              <div class="method-source-code" id="uuid-source">
            <pre><span class="ruby-comment"># File random/formatter.rb, line 169</span>
<span class="ruby-keyword">def</span> <span class="ruby-identifier ruby-title">uuid</span>
  <span class="ruby-identifier">ary</span> = <span class="ruby-identifier">random_bytes</span>(<span class="ruby-value">16</span>).<span class="ruby-identifier">unpack</span>(<span class="ruby-string">&quot;NnnnnN&quot;</span>)
  <span class="ruby-identifier">ary</span>[<span class="ruby-value">2</span>] = (<span class="ruby-identifier">ary</span>[<span class="ruby-value">2</span>] <span class="ruby-operator">&amp;</span> <span class="ruby-value">0x0fff</span>) <span class="ruby-operator">|</span> <span class="ruby-value">0x4000</span>
  <span class="ruby-identifier">ary</span>[<span class="ruby-value">3</span>] = (<span class="ruby-identifier">ary</span>[<span class="ruby-value">3</span>] <span class="ruby-operator">&amp;</span> <span class="ruby-value">0x3fff</span>) <span class="ruby-operator">|</span> <span class="ruby-value">0x8000</span>
  <span class="ruby-string">&quot;%08x-%04x-%04x-%04x-%04x%08x&quot;</span> <span class="ruby-operator">%</span> <span class="ruby-identifier">ary</span>
<span class="ruby-keyword">end</span></pre>
                              </div>
                            </div>


                          </div>

                          </section>

                <section id="private-instance-5Buntitled-5D-method-details" class="method-section">
                <header>
                <h3>Private Instance Methods</h3>
                </header>

                  <div id="method-i-choose" class="method-detail ">
                            <div class="method-heading">
                              <span class="method-name">choose</span><span
                                class="method-args">(source, n)</span>
                              <span class="method-click-advice">click to toggle source</span>
                            </div>

                            <div class="method-description">
                              <p>Generate a string that randomly draws from a source array of characters.</p>

<p>The argument <em>source</em> specifies the array of characters from which to generate the string. The argument <em>n</em> specifies the length, in characters, of the string to be generated.</p>

<p>The result may contain whatever characters are in the source array.</p>

<pre class="ruby"><span class="ruby-identifier">require</span> <span class="ruby-string">&#39;random/formatter&#39;</span>

<span class="ruby-identifier">prng</span>.<span class="ruby-identifier">choose</span>([<span class="ruby-operator">*</span><span class="ruby-string">&#39;l&#39;</span><span class="ruby-operator">..</span><span class="ruby-string">&#39;r&#39;</span>], <span class="ruby-value">16</span>) <span class="ruby-comment">#=&gt; &quot;lmrqpoonmmlqlron&quot;</span>
<span class="ruby-identifier">prng</span>.<span class="ruby-identifier">choose</span>([<span class="ruby-operator">*</span><span class="ruby-string">&#39;0&#39;</span><span class="ruby-operator">..</span><span class="ruby-string">&#39;9&#39;</span>], <span class="ruby-value">5</span>)  <span class="ruby-comment">#=&gt; &quot;27309&quot;</span>
</pre>

                              <div class="method-source-code" id="choose-source">
            <pre><span class="ruby-comment"># File random/formatter.rb, line 194</span>
        <span class="ruby-keyword">def</span> <span class="ruby-identifier ruby-title">choose</span>(<span class="ruby-identifier">source</span>, <span class="ruby-identifier">n</span>)
  <span class="ruby-identifier">size</span> = <span class="ruby-identifier">source</span>.<span class="ruby-identifier">size</span>
  <span class="ruby-identifier">m</span> = <span class="ruby-value">1</span>
  <span class="ruby-identifier">limit</span> = <span class="ruby-identifier">size</span>
  <span class="ruby-keyword">while</span> <span class="ruby-identifier">limit</span> <span class="ruby-operator">*</span> <span class="ruby-identifier">size</span> <span class="ruby-operator">&lt;=</span> <span class="ruby-value">0x100000000</span>
    <span class="ruby-identifier">limit</span> <span class="ruby-operator">*=</span> <span class="ruby-identifier">size</span>
    <span class="ruby-identifier">m</span> <span class="ruby-operator">+=</span> <span class="ruby-value">1</span>
  <span class="ruby-keyword">end</span>
  <span class="ruby-identifier">result</span> = <span class="ruby-string">&#39;&#39;</span>.<span class="ruby-identifier">dup</span>
  <span class="ruby-keyword">while</span> <span class="ruby-identifier">m</span> <span class="ruby-operator">&lt;=</span> <span class="ruby-identifier">n</span>
    <span class="ruby-identifier">rs</span> = <span class="ruby-identifier">random_number</span>(<span class="ruby-identifier">limit</span>)
    <span class="ruby-identifier">is</span> = <span class="ruby-identifier">rs</span>.<span class="ruby-identifier">digits</span>(<span class="ruby-identifier">size</span>)
    (<span class="ruby-identifier">m</span><span class="ruby-operator">-</span><span class="ruby-identifier">is</span>.<span class="ruby-identifier">length</span>).<span class="ruby-identifier">times</span> { <span class="ruby-identifier">is</span> <span class="ruby-operator">&lt;&lt;</span> <span class="ruby-value">0</span> }
    <span class="ruby-identifier">result</span> <span class="ruby-operator">&lt;&lt;</span> <span class="ruby-identifier">source</span>.<span class="ruby-identifier">values_at</span>(<span class="ruby-operator">*</span><span class="ruby-identifier">is</span>).<span class="ruby-identifier">join</span>(<span class="ruby-string">&#39;&#39;</span>)
    <span class="ruby-identifier">n</span> <span class="ruby-operator">-=</span> <span class="ruby-identifier">m</span>
  <span class="ruby-keyword">end</span>
  <span class="ruby-keyword">if</span> <span class="ruby-value">0</span> <span class="ruby-operator">&lt;</span> <span class="ruby-identifier">n</span>
    <span class="ruby-identifier">rs</span> = <span class="ruby-identifier">random_number</span>(<span class="ruby-identifier">limit</span>)
    <span class="ruby-identifier">is</span> = <span class="ruby-identifier">rs</span>.<span class="ruby-identifier">digits</span>(<span class="ruby-identifier">size</span>)
    <span class="ruby-keyword">if</span> <span class="ruby-identifier">is</span>.<span class="ruby-identifier">length</span> <span class="ruby-operator">&lt;</span> <span class="ruby-identifier">n</span>
      (<span class="ruby-identifier">n</span><span class="ruby-operator">-</span><span class="ruby-identifier">is</span>.<span class="ruby-identifier">length</span>).<span class="ruby-identifier">times</span> { <span class="ruby-identifier">is</span> <span class="ruby-operator">&lt;&lt;</span> <span class="ruby-value">0</span> }
    <span class="ruby-keyword">else</span>
      <span class="ruby-identifier">is</span>.<span class="ruby-identifier">pop</span> <span class="ruby-keyword">while</span> <span class="ruby-identifier">n</span> <span class="ruby-operator">&lt;</span> <span class="ruby-identifier">is</span>.<span class="ruby-identifier">length</span>
    <span class="ruby-keyword">end</span>
    <span class="ruby-identifier">result</span>.<span class="ruby-identifier">concat</span> <span class="ruby-identifier">source</span>.<span class="ruby-identifier">values_at</span>(<span class="ruby-operator">*</span><span class="ruby-identifier">is</span>).<span class="ruby-identifier">join</span>(<span class="ruby-string">&#39;&#39;</span>)
  <span class="ruby-keyword">end</span>
  <span class="ruby-identifier">result</span>
<span class="ruby-keyword">end</span></pre>
                              </div>
                            </div>


                          </div>

                  <div id="method-i-gen_random" class="method-detail ">
                            <div class="method-heading">
                              <span class="method-name">gen_random</span><span
                                class="method-args">(n)</span>
                              <span class="method-click-advice">click to toggle source</span>
                            </div>

                            <div class="method-description">
                              

                              <div class="method-source-code" id="gen_random-source">
            <pre><span class="ruby-comment"># File random/formatter.rb, line 176</span>
        <span class="ruby-keyword">def</span> <span class="ruby-identifier ruby-title">gen_random</span>(<span class="ruby-identifier">n</span>)
  <span class="ruby-keyword">self</span>.<span class="ruby-identifier">bytes</span>(<span class="ruby-identifier">n</span>)
<span class="ruby-keyword">end</span></pre>
                              </div>
                            </div>


                          </div>

                          </section>

              </section>
              </main>



            </div>  <!--  class='wrapper hdiv' -->


<footer id="validator-badges" role="contentinfo">
<p><a href="https://validator.w3.org/check/referer">Validate</a></p>
<p>Generated by <a href="https://ruby.github.io/rdoc/">RDoc</a> 6.4.0.</p>
<p>Based on <a href="https://github.com/ged/darkfish/">Darkfish</a> by <a href="http://deveiate.org">Michael Granger</a>.</p>

  
    <p><p><a href="https://ruby-doc.org">Ruby-doc.org</a> is provided by <a href="https://jamesbritt.com">James Britt</a> and <a href="https://neurogami.com">Neurogami</a>.</p><p><a href="https://jamesbritt.bandcamp.com/">Maximum R+D</a>.  </p>
</p>
  
  </footer>

<script type="text/javascript">


  let ads  = $("#carbonads-container").children().detach();


  function swapMode() {
    var cookieName = 'darkmode';
    var cssDarkmode = Cookies.get(cookieName);
    console.log("***** swapMode! " + cssDarkmode + " *****");


    if (cssDarkmode == "true") {
      console.log("We have dark mode, set the css to light ...");
      $('#rdoccss').attr("href", defaultModeCssHref);
      $('#cssSelect').text("Dark mode");
      cssDarkmode = "false";
      console.log("swapMode! Now set cookie to " + cssDarkmode);
      Cookies.set(cookieName, cssDarkmode);

    } else {
      console.log("We not have dark mode, set the css to dark ...");
      $('#rdoccss').attr("href", darkModeCsseHref);
      $('#cssSelect').text("Light mode");
      cssDarkmode = "true";
      console.log("swapMode! Now set cookie to " + cssDarkmode);
      Cookies.set(cookieName, cssDarkmode);

    }

    console.log("  --------------- ");
  }


const vueCssApp = new Vue({
el: '#menubar',
data: {
isDark: false
},
methods: {
toggleClass: function(event){
this.isDark = !this.isDark;
}
}
})

const vueApp = new Vue({
el: '#vapp',
data: { 
isOpen: true
},

mounted() {
this.handleResize();
this.manage_mob_classes();
window.addEventListener('resize', this.handleResize)
//this.isOpen !=  (/Android|webOS|iPhone|iPad|iPod|BlackBerry|IEMobile|Opera Mini/i.test(navigator.userAgent));
},
destroyed() {
window.removeEventListener('resize', this.handleResize)
},
created() {
//manage_mob_classes();
},

methods : {
isMobile() {
  return (/Android|webOS|iPhone|iPad|iPod|BlackBerry|IEMobile|Opera Mini/i.test(navigator.userAgent));
},

  handleResize() {
    if (!this.isMobile()) {
      this.isOpen = window.innerWidth > 800;
    }
  },

  manage_mob_classes() {
    if (/Android|webOS|iPhone|iPad|iPod|BlackBerry|IEMobile|Opera Mini/i.test(navigator.userAgent)) {
      $("nav").addClass("mob_nav");
      $("main").addClass("mob_main");
      $("#extraz").addClass("mob_extraz");
      $("#carbonads-container").addClass("mob_carbonads-container");
      this.isOpen  = false;
    } else {
      $("nav").removeClass("mob_nav") 
        $("main").removeClass("mob_main");
      $("#extraz").removeClass("mob_extraz");
      $("#carbonads-container").removeClass("mob_carbonads-container");
      this.isOpen  = true;
    }
  },

  toggleNav() {
    this.isOpen =! this.isOpen ;
    // alert("Toggle nav!");
    console.log("toggleNav() click: " + this.isOpen );
  }
}
})

$("#carbonads-container").append(ads);


$(function() {

    var darkmode = Cookies.get("darkmode");
    console.log("Document ready: " + darkmode);

    if ( darkmode  == "true" ) {
      $('#cssSelect').text("Light mode");
    } else {
      $('#cssSelect').text("Dark mode");
     }

    $('body').css('display','block');
    });

</script>

    
  </body> 
</html>

